Report post

Why does array_push() raise a warning if the first argument is not an array?

Note: If you use array_push () to add one element to the array, it's better to use $array = because in that way there is no overhead of calling a function. Note: array_push () will raise a warning if the first argument is not an array. This differed from the $var behaviour where a new array was created, prior to PHP 7.1.0. The input array.

Does array_push check if a variable is an array first?

Empy bracket doesn't check if a variable is an array first as array_push does. If array_push finds that a variable isn't an array it prints a Warning message if E_ALL error reporting is on. So array_push is safer than , until further this is changed by the PHP developers.

Why is array_push slower than core array-access?

Since " array_push " is a function and it called multiple times when it is inside the loop, it will allocate memory into the stack. But when we are using $array = $value then we are just assigning a value to the array. Second one is a function call so generally it should be slower than using core array-access features.

Is there an array_push() equivalent for associative arrays?

Nope, there is no array_push () equivalent for associative arrays because there is no way determine the next key. @KingGoeks $arrayname = array ('indexname1' => $value1, 'indexname2' => $value2); would set them as the only items in $arrayname. If you already have $arrayname set and want to keep its values, try $arrayname += $anotherarray.

Related articles

The World's Leading Crypto Trading Platform

Get my welcome gifts